import json
from tkinter import *
from tkinter import ttk

from data.protocol_text.protocol_1FH import protocols_1FH
from data.protocol_text.protocol_20H import protocols_20H
from data.protocol_text.protocol_21H import protocols_21H
from data.protocol_text.protocol_23H import protocols_23H
from data.protocol_text.protocol_33H import protocols_33H
from data.protocol_text.protocol_37H import protocols_37H
from data.protocol_text.protocol_38H import protocols_38H
from utils.protocol_util import StartSplit, slice_string, hex_to_ascii, hex_to_decimal, list_of_reverse_chars
from utils.thinkter_util import TkUtils


class StationConverseToChargerView:

    def __init__(self):
        self.root = Tk()
        self.root.title('站控与充电系统通信协议转换')
        screenwidth = self.root.winfo_screenwidth()  # 获取显示屏宽度
        screenheight = self.root.winfo_screenheight()  # 获取显示屏高度
        size = '%dx%d+%d+%d' % (650, 600, (screenwidth - 650) / 2, (screenheight - 650) / 2)
        self.root.resizable(0, 0)
        self.root.geometry(size)
        self.tim = 30

    def station_converse_to_charger(self):
        class Commands:
            def conversion(self):
                entry_data = text_box_before.get(1.0, 'end')
                try:
                    if entry_data != None:
                        if '23H' in get_value_from_checkbox():
                            protocols_string = protocols_23H
                        elif '20H' in get_value_from_checkbox():
                            protocols_string = protocols_20H
                        elif '1FH' in get_value_from_checkbox():
                            protocols_string = protocols_1FH
                        elif '33H' in get_value_from_checkbox():
                            protocols_string = protocols_33H
                        elif '37H' in get_value_from_checkbox():
                            protocols_string = protocols_37H
                        elif '38H' in get_value_from_checkbox():
                            protocols_string = protocols_38H
                        elif '21H' in get_value_from_checkbox():
                            protocols_string = protocols_21H
                        text_box_after.delete(1.0, 'end')
                        data = StartSplit(protocol_string=protocols_string, str_datas=entry_data).start_split()
                        insert_data = json.dumps(data, indent=4, sort_keys=False, ensure_ascii=False)
                        text_box_after.insert('end', insert_data)
                except Exception:
                    test = '报文类型错误,请检查报文！'
                    text_box_after.insert('end', test)

            def clear(self):
                text_box_before.delete(1.0, 'end')
                text_box_after.delete(1.0, 'end')
                text_after_ascii.delete(1.0, 'end')
                text_after_hex.delete(1.0, 'end')
                text_box_queery.delete(1.0, 'end')
                entry_text1.set('')
                entry_text2.set('')
                entry_before_hex.set('')
                text_box_reverse.delete(1.0, 'end')

            def to_ascii(self):
                before_datas = entry_before_hex.get()
                ascii_data = hex_to_ascii(before_datas)
                text_after_ascii.delete(1.0, 'end')
                text_after_ascii.insert('end', ascii_data)

            def to_decimal(self):
                before_data = entry_before_hex.get()
                decimal_data = hex_to_decimal(before_data)
                text_after_hex.delete(1.0, 'end')
                text_after_hex.insert('end', decimal_data)

            def reverse_data(self):
                text_box_reverse.delete(1.0, 'end')
                before_data = text_box_queery.get(1.0, 'end')
                reverse_data = list_of_reverse_chars(before_data)
                text_box_reverse.delete(1.0, 'end')
                text_box_reverse.insert('end', reverse_data)

            def query(self):
                try:
                    entry_data = text_box_before.get(1.0, 'end')
                    start_index = entry_text1.get()
                    length = entry_text2.get()
                    data = slice_string(str_data=entry_data, start_index=int(start_index), length=int(length))
                    text_box_queery.delete(1.0, 'end')
                    text_box_queery.insert('end', data)
                except Exception:
                    text_box_queery.delete(1.0, 'end')
                    text_box_queery.insert('end', '请在左侧输入查询必要条件')

        TkUtils().label(master=self.root, text="请选择解析的报文类型：", fg="black", row=0, column=0)
        # 创建下拉框
        dropdown = StringVar(self.root)
        dropdown.set("请选择报文类型")
        dropdown_box = ttk.Combobox(self.root, textvariable=dropdown,
                                    values=["1FH-启动充电", "23H-最新充电订单", "20H-回复启动充电",
                                            "21H-启动充电结果", "33H-历史充电订单", "37H-设置计费模型",
                                            "38H-回复计费模型设置"])

        def get_value_from_checkbox(*args):
            return dropdown_box.get()

        dropdown_box.bind("<<ComboboxSelected>>", get_value_from_checkbox)
        dropdown_box.grid(row=0, column=1)
        TkUtils().label(master=self.root, text="请输入要解析的源码：", fg="black", row=1, column=0)
        # 创建转义后文本框
        text_box_before = Text(self.root, height=25, width=25)
        text_box_before.grid(row=2, column=0, sticky=W, padx=5, pady=5)

        TkUtils().label(master=self.root, text="解析结果：", fg="black", row=1, column=1)
        text_box_after = Text(self.root, height=25, width=50)
        text_box_after.grid(row=2, column=1)
        #
        TkUtils().label(master=self.root, text="请输入起始索引、长度：", fg="black", row=3, column=0)
        entry_text1 = StringVar(self.root)
        enter = Entry(master=self.root, textvariable=entry_text1, width=5)
        enter.place(x=25, y=450)
        entry_text2 = StringVar(self.root)
        enter = Entry(master=self.root, textvariable=entry_text2, width=5)
        enter.place(x=80, y=450)
        TkUtils().label(master=self.root, text="查询结果：", fg="black", row=3, column=1)
        text_box_queery = Text(self.root, height=2, width=20)
        text_box_queery.place(x=200, y=450)
        TkUtils().labels(master=self.root, text="翻转结果：", fg="black", x=400, y=415)
        text_box_reverse = Text(self.root, height=2, width=20)
        text_box_reverse.place(x=400, y=450)

        TkUtils().labels(master=self.root, text="请输入16进制原始报文：", fg="black", x=0, y=480)
        entry_before_hex = StringVar(self.root)
        enter = Entry(master=self.root, textvariable=entry_before_hex, width=25)
        enter.place(x=10, y=510)
        TkUtils().labels(master=self.root, text="10进制：", fg="black", x=230, y=480)
        text_after_hex = Text(self.root, height=2, width=20)
        text_after_hex.place(x=200, y=510)
        TkUtils().labels(master=self.root, text="ASCII：", fg="black", x=410, y=480)
        text_after_ascii = Text(self.root, height=2, width=20)
        text_after_ascii.place(x=400, y=510)

        TkUtils().button(master=self.root, text="解析", fg="blue", x=140, y=45, command=Commands().conversion)
        TkUtils().button(master=self.root, text="清除", fg="red", x=100, y=550, command=Commands().clear)
        TkUtils().button(master=self.root, text="查询", fg="green", x=140, y=450, command=Commands().query)
        TkUtils().button(master=self.root, text="翻转", fg="blue", x=350, y=450, command=Commands().reverse_data)
        TkUtils().button(master=self.root, text="Ten", fg="red", x=260, y=550, command=Commands().to_decimal)
        TkUtils().button(master=self.root, text="Ascii", fg="green", x=420, y=550, command=Commands().to_ascii)
        self.root.mainloop()


if __name__ == '__main__':
    StationConverseToChargerView().station_converse_to_charger()
